/**
 * Created by joy on 03/09/2017.
 */
import BitcoinCashWalletHelper from '../../common/helpers/chain/BitcoinCashWalletHelper'
import CommonUtil from '../../common/utils/CommonUtil'
import prompt from 'prompt'
import fs from 'fs'
import util from 'util'

export default async () => {
  const bitcoinCashWalletHelper = new BitcoinCashWalletHelper()
  const schema = {
    properties: {
      xpub: {
        description: 'Enter your master public key',
        type: 'string',
        required: true
      },
      pathSpan: {
        description: 'Enter path span',
        type: 'string',
        required: true,
        default: 'm/0/0/1~10'
      },
      output: {
        description: 'Enter filename to save result',
        type: 'string',
        required: true,
        default: 'temp_data/addresses_%s.txt'
      }
    }
  }

  const args = await CommonUtil.getArgsFromConsole(prompt, schema)
  const { xpub, pathSpan, output } = args
  const addresses = await bitcoinCashWalletHelper.getAddressesFromXpub(xpub, pathSpan, 'testnet')
  const data = addresses.map((address) => {
    return `${address['path']}:${address['address']}`
  }).join('\n')
  fs.writeFileSync(util.format(output, pathSpan.replace(/\//g, '_')), data)
}
